package org.github.manx98.jlibhac.fssystem;

import cn.hutool.core.util.HexUtil;
import org.github.manx98.jlibhac.fs.IStorage;

import java.io.IOException;

public class PartitionFileEntry {
    public final int index;
    public final long offset;
    public final long size;
    public final long stringTableOffset;
    public long hashedRegionOffset;
    public int hashedRegionSize;
    public final String hash;
    public String name;

    public PartitionFileEntry(IStorage reader, PartitionFileSystemType type, int index) throws IOException {
        this.index = index;
        offset = reader.readLong();
        size = reader.readLong();
        stringTableOffset = reader.readUint32().longValue();
        if (type == PartitionFileSystemType.Hashed) {
            hashedRegionSize = reader.readInt();
            hashedRegionOffset = reader.readLong();
            hash = HexUtil.encodeHexStr(reader.readBytes(0x20));
        } else {
            hash = null;
            reader.readBytes(4);
        }
    }

    @Override
    public String toString() {
        return "PartitionFileEntry{" +
                "index=" + index +
                ", offset=" + offset +
                ", size=" + size +
                ", stringTableOffset=" + stringTableOffset +
                ", hashedRegionOffset=" + hashedRegionOffset +
                ", hashedRegionSize=" + hashedRegionSize +
                ", hash='" + hash + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
